<template>
 <div>
    <el-row type="flex" class="row-bg" justify="space-between">
        <el-col :span="10">
          <el-form-item label="省份">
            <el-select v-model="form.province"
            :disabled='readOnly'
            @visible-change='vChang'
             @change="change(form.province,1)"
             filterable placeholder="请选择省份">
              <el-option v-for="(pd,index) in proList" :key="index" :label="pd.name" :value="pd.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="城市">
            <el-select v-model="form.city"
              filterable
            @change="change(form.city,2)"
            @visible-change='vChang'

            :disabled='readOnly'
             placeholder="请选择城市">
              <el-option v-for="(cd,index) in cityList"
               :key="index"
                :label="cd.name"
                 :value="cd.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex"
      v-if="showArea"
       class="row-bg" justify="space-between">
        <el-col :span="10">
            <el-form-item label="地区">
            <el-select v-model="form.area"
              filterable
            @change="change(form.area,3)"
            @visible-change='vChang'
            :disabled='readOnly'
             placeholder="请选择地区">
              <el-option v-for="(ad,index) in areaList"
               :key="index"
                :label="ad.name" :value="ad.code"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
 </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  props: {
    readOnly: {
      type: Boolean,
      default: false
    },
    showArea: { type: Boolean, default: true },
    parentForm: {
      type: [Object, Array],
      default: () => ({})
    }
  },
  data() {
    return {
      form: {
        area: "",
        city: "",
        province: ""
      },
      isEdit: false
    };
  },
  beforeMount() {
    this.$store.dispatch("setCityList", { code: "", type: "" });
    if (this.$route.meta.type == "edit") {
      this.isEdit = true;
    }
  },
  methods: {
    vChang() {},
    change(itemCode, type) {
      this.activeType = type;
      this.$emit("changeCity", this.form);
      if (type <= 2) {
        this.$store.dispatch("setCityList", {
          code: itemCode,
          type: type
        });
      }
    }
  },
  computed: {
    ...mapGetters({
      proList: "proList",
      cityList: "cityList",
      areaList: "areaList"
    })
  },
  watch: {
    parentForm() {
        if (this.parentForm.province) {
          this.$store.dispatch("setCityList", {
            code: this.parentForm.province,
            type: 1
          });
        }
        if (this.form.city) {
          this.$store.dispatch("setCityList", {
            code: this.parentForm.city,
            type: 2
          });
        }
      this.form = { ...this.form, ...this.parentForm };
    }
  }
};
</script>

<style>
</style>
